using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace SimplePong
{
    class GameScreen : Screen
    {


        Ball ball;
        Player player1;
        PlayerIA player2;

        int score1;
        int score2;
        Vector2 posScore1;
        Vector2 posScore2;

        Texture2D background;

        SoundEffect boundSound;
        SpriteFont scoreFont;

        #region Properties

        #endregion

        #region Initialize
        public GameScreen()
            : base()
        {
            
        }

        #endregion

        #region Public Methods
        public override void LoadContent()
        {
            scoreFont = GameGlobals.content.Load<SpriteFont>("Fonts/score");
            background = GameGlobals.content.Load<Texture2D>("Textures/background");
            boundSound = GameGlobals.content.Load<SoundEffect>("Sounds/a");
        }

        public override void Initialize()
        {
            ball = new Ball();
            player1 = new Player(1);
            player2 = new PlayerIA(2);

            score1 = score2 = 0;

        }

        public override void Update()
        {

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                ScreenManager.TransitionTo("MainMenu", "BlackFade");



            ball.Update();
            player1.Update();
            player2.Update(ball);

            if (ball.Rectangle.Intersects(player1.Rectangle))
            {
                ball.VelocityX *= -1;
                ball.PositionX = player1.Position.X + player1.Rectangle.Width;
                ball.IncreaseSpeed();
                boundSound.Play();
            }
            if (ball.Rectangle.Intersects(player2.Rectangle))
            {
                ball.VelocityX *= -1;
                ball.PositionX = player2.Position.X - player2.Rectangle.Width;
                ball.IncreaseSpeed();
                boundSound.Play();
            }

            if (ball.Position.X <= 0)
            {
                score2++;
                ball.ResetBall();
            }

            if (ball.Position.X + ball.Rectangle.Width >= GameGlobals.graphicsManager.PreferredBackBufferWidth)
            {
                score1++;
                ball.ResetBall();
            }

        }

        public override void Draw()
        {
            spriteBatch.Begin();
            spriteBatch.Draw(background, Vector2.Zero, Color.White);
            ball.Draw(spriteBatch);
            player1.Draw(spriteBatch);
            player2.Draw(spriteBatch);


            Vector2 mesureString = scoreFont.MeasureString(score1.ToString());
            posScore1 = new Vector2(GameGlobals.graphicsManager.PreferredBackBufferWidth/2 - mesureString.X - 50,
                                5 + mesureString.Y);

            mesureString = scoreFont.MeasureString(score2.ToString());
            posScore2 = new Vector2(GameGlobals.graphicsManager.PreferredBackBufferWidth / 2 + 50,
                                5 + mesureString.Y);


            spriteBatch.DrawString(scoreFont, score1.ToString(), posScore1, Color.White);
            spriteBatch.DrawString(scoreFont, score2.ToString(), posScore2, Color.White);

            spriteBatch.End();
        }

        #endregion

        #region Private Methods
        #endregion
    }
}
